package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.bean.RoomAddPlayListResponse;
import com.ledim.datastatistics.LeDimAnalysisUtils;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimBaseUtils;
import com.ledim.utils.LedimFunctionUtils;
import com.ledim.utils.ToastUtil;
import com.letv.android.client.LetvSDK;
import com.letv.android.young.client.R;
import com.letv.core.bean.IVideo;

/**
 * Created by GeekZooStudio on 2015/10/10.
 */
public class SearchRelatedListViewItemView extends LinearLayout
{

	private static final int POSITIVE = 180001;
	private static final int TV = 181031;
	private View mLayout;
	private SimpleDraweeView mImage;
	private TextView mTitle;
	private TextView mNum;
	private TextView mLen;
	private Context mContext;
	private boolean mAddPlay;
	private String mRoomId;
	private LedimChoiceCardBean card;
	private String mMediaId;
	private String mSubMediaId;

	public SearchRelatedListViewItemView(Context context)
	{
		super(context);
		mContext = context;
	}

	public SearchRelatedListViewItemView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		mContext = context;
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public SearchRelatedListViewItemView(Context context, AttributeSet attrs,
	        int defStyle)
	{
		super(context, attrs, defStyle);
		mContext = context;
	}

	@Override
	protected void onFinishInflate()
	{
		super.onFinishInflate();
	}

	// 绑定数据
	public void bindData(LedimChoiceCardBean card, String roomId)
	{
		this.card = card;
		mRoomId = roomId;
		mAddPlay = !TextUtils.isEmpty(roomId);
		String url = card.link.replaceAll("app://medias/", "");
		mMediaId = url.substring(0, url.indexOf("/"));

		mSubMediaId = card.link.substring(card.link.lastIndexOf("/") + 1,
		        card.link.length());
		card.submedia = mSubMediaId;
		card.media = mMediaId;
		init();
	}

	/**
	 * 初始化界面
	 */
	private void init()
	{
		mLayout = findViewById(R.id.all_layout);
		mImage = (SimpleDraweeView) findViewById(R.id.search_result_list_img);
		mTitle = (TextView) findViewById(R.id.search_result_list_title);
		mNum = (TextView) findViewById(R.id.search_result_list_num);
		mLen = (TextView) findViewById(R.id.search_result_list_length);

		if (card.photo != null && !TextUtils.isEmpty(card.photo.thumb))
		{
			FrescoUtil.loadUrl(card.photo.thumb, mImage);
		}
		mTitle.setText(Html.fromHtml(card.title));
		setVideoPlayCount(mNum, card.video.playCount);

		if (!TextUtils.isEmpty(card.video.duration))
		{
			mLen.setVisibility(View.VISIBLE);
			mLen.setText(LedimBaseUtils.secToTime(card.video.duration));
		}
		else
		{
			mLen.setVisibility(View.GONE);
		}

		if (mAddPlay)
		{
			mLayout.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					// 上报数据
					LedimFunctionUtils.reportSearchItemEvent(
					        LeDimAnalysisUtils.ROOM_SEARCH_CODE, card);

					if (mMediaId.equals("0"))
					{
						mLayout.setBackgroundColor(Color.parseColor("#FEFBBD"));
						addRoomPlayLists(mMediaId, mSubMediaId);
					}
					else
					{
						mLayout.setBackgroundColor(Color.parseColor("#FEFBBD"));
						addRoomPlayLists("0", mSubMediaId);
						// mMediasTypeMediaSub_mediaEpisodesGetModel.getMediaLists(SearchRelatedListViewItemView.this,
						// mMediaId,mSubMediaId,2,true,0,mAddPlay);
					}

				}
			});
		}
		else
		{
			mLayout.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					// 上报数据
					LedimFunctionUtils.reportSearchItemEvent(
					        LeDimAnalysisUtils.ALBUM_SEARCH_CODE, card);
					IVideo video = new IVideo();
					video.setmVid(0);
					LetvSDK.getInstance().play(mContext, video, false, card,
					        false);// card这是一个CARD对象，调用此方法后将CARD传给AlbumPlayActivity中

				}
			});
		}

	}

	private void addRoomPlayLists(String mMediaId, String mSubMediaId)
	{
		LedimApiServiceUtils.addRoomPlayListByVid(mRoomId, mMediaId,
		        mSubMediaId, card.title,
		        card.video == null || card.video.duration == null ? "0"
		                : card.video.duration,
		        observer);
	}

	// 设置播放次数
	private void setVideoPlayCount(TextView mNum, String playCount)
	{
		if (playCount.equals("null"))
		{
			mNum.setVisibility(View.GONE);
		}
		else
		{
			mNum.setVisibility(View.VISIBLE);
			Long count = Long.parseLong(playCount);
			if (count > 0)
			{
				mNum.setText(LedimFunctionUtils.formatGoldCount(count) + "次");
			}
			else
			{
				mNum.setText("");
			}
		}

	}

	private LedimHttpObserver observer = new LedimHttpObserver<RoomAddPlayListResponse>()
	{
		@Override
		public void onError(Throwable e)
		{
			super.onError(e);
			ToastUtil.toast("添加失败,请稍候重试");
		}

		@Override
		public void onNext(RoomAddPlayListResponse response)
		{
			super.onNext(response);
			if (response != null && response.success)
			{
				ToastUtil.toast("已经添加到放映单");
			}
			else
			{
				if (response.code == 404)
				{
					ToastUtil.toast("内容太高能,添加失败");
				}
				else
				{
					ToastUtil.toast("添加失败,请稍候重试");
				}
			}

		}
	};

	/**
	 * 上报item点击的个数
	 */
	private void reportItemClickEvent()
	{

	}
}
